package com.yyq.sos.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.jasig.cas.client.validation.Assertion;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yyq.adms.usermanage.bpo.SysOnlineUsersBpo;
import com.yyq.adms.usermanage.bpo.SysUsersBpo;
import com.yyq.adms.usermanage.pojo.SysOnlineUsers;
import com.yyq.servlets.ApplicationContextHandle;
import com.yyq.sos.permission.LoginUser;
import com.yyq.sos.util.UserUtil;


/**
 * 自动根据单点登录系统的信息设置本系统的用户信�?
 *
 * @author snepajj
 * 
 *
 */
public class AutoSetUserAdapterFilter implements Filter {
	
	/**
	 * Default constructor. 
	 */
	public AutoSetUserAdapterFilter() {
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
	}

	/**
	 * 过滤逻辑：首先判断单点登录的账户是否已经存在本系统中�?
	 * 如果不存在使用用户查询接口查询出用户对象并设置在Session�?
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		
		// _const_cas_assertion_是CAS中存放登录用户名的session标志
		Object object = httpRequest.getSession().getAttribute("_const_cas_assertion_");
		
		if (object != null) {
			Assertion assertion = (Assertion) object;
			String loginName = assertion.getPrincipal().getName();
			LoginUser loginUser = UserUtil.getCurrentUser(httpRequest.getSession());
			String ip =null;
			if (httpRequest.getHeader("x-forwarded-for") == null) {
				ip = httpRequest.getRemoteAddr();
			}else {
				ip = httpRequest.getHeader("x-forwarded-for");
			}
			
			// 第一次登录系�?
			if (loginUser == null) {
				loginUser = new LoginUser(loginName);
				loginUser.setIp(ip);
				// 保存用户信息到Session
				UserUtil.saveUserToSession(httpRequest.getSession(), loginUser);
				SysOnlineUsers onlineUsers=new SysOnlineUsers();
				onlineUsers.setUserId(loginUser.getUserId());
				onlineUsers.setUsername(loginUser.getUserName());
				onlineUsers.setOnlinests(1);
//				onlineUsers.setIp(this.getRequest().getRemoteAddr());
				onlineUsers.setIp(ip);
				onlineUsers.setMachinename(httpRequest.getRemoteHost());
				SysOnlineUsersBpo onlineBpo = (SysOnlineUsersBpo)ApplicationContextHandle.getBean("sysOnlineUsersBpo");
				onlineBpo.setCls(onlineUsers.getClass());
				Object obj = onlineBpo.findByPk(loginUser.getUserId());
				if(obj == null){
					onlineBpo.save(onlineUsers);
				}
			}
			
		}
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
	}

}
